package com.sumainfor.common.UnionPay;

import java.io.UnsupportedEncodingException;
import java.security.*;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * Created by zwj on 2018/8/11.
 * 只能私钥加签，公钥验签
 */
public class SignUtils {
    /**
     * 生成签名
     *
     * @param params
     * @param privateKey
     * @return
     */
    public static void getSign(Map<String, String> params, String privateKey) {
        MapUtil.cleanNull(params);
        if (params == null||params.size()<1) {
            return ;
        }
        String content = getContent(params);//待签名的内容
        try {
            PKCS8EncodedKeySpec priPKCS8 = new PKCS8EncodedKeySpec(Base64.decode(privateKey));
            KeyFactory keyf = KeyFactory.getInstance("RSA");
            PrivateKey priKey = keyf.generatePrivate(priPKCS8);

            Signature signature = Signature.getInstance("SHA256WithRSA");

            signature.initSign(priKey);
            signature.update(content.getBytes("UTF-8"));
            byte[] signed = signature.sign();
            params.put("sign",Base64.encode(signed));
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (SignatureException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (InvalidKeySpecException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }

    private static String getContent(Map<String, String> params) {
        List<String> keys = new ArrayList<String>(params.keySet());
        Collections.sort(keys);
        StringBuffer sb = new StringBuffer();
        for (String key : keys) {
            sb.append(key + "=");
            String value = params.get(key);
            if (value != null && !"".equals(value.trim())) {
                sb.append(value + "&");
            }
        }
        String content = sb.toString();
        content = content.substring(0, content.length() - 1);
        return content;
    }

    /**
     * 验签
     *
     * @param params
     * @return
     */
    public static boolean verifySign(Map<String, String> params, String publicKey) {
        MapUtil.cleanNull(params);
        try {
            String sign = params.get("sign");
            params.remove("sign");
            byte[] b = (new sun.misc.BASE64Decoder()).decodeBuffer(publicKey);
            X509EncodedKeySpec keySpec = new X509EncodedKeySpec(b);
            KeyFactory factoty = KeyFactory.getInstance("RSA");
            PublicKey pubKey = factoty.generatePublic(keySpec);
            Signature signature = Signature.getInstance("SHA256WithRSA");
            signature.initVerify(pubKey);
            signature.update(getContent(params).getBytes("UTF-8"));
            boolean bverify = signature.verify(Base64.decode(sign));
            return bverify;
        } catch (Exception e) {
            return false;
        }
    }

    private static String publickey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCdCsSdp4iJS5xD9ZVXa4UbtLSFaelIh+ulLpkKAmbniUqz1ZRL0EbqmZInWEGj0ap1MkoQQ6Oe9sLj41qlfR+LLRY5PXbxG/kZ5rBe9hE5Nv322n8b1Xezwxy+7CLwtxrq4549HOKU0t/g4rvt9qq7c+CsqlyJIrWSQCWG+rYHBQIDAQAB";
    static String data = "data=NUUyOUU1QTFGODdEQkQzNTUzNDAxNDUxREI2NUEyNjFFOTIwNzdGQTJCMzQ0MDVCNDk4QUUwM0QyMTFBODg5QkJCRUE0MzlBNjE1NEIxQzdFRUM2Qjk0RDM3NUVBMEE0ODQxNThBMTE2MzZGNTE5RTc4NkNGMzUwM0Y0QzM4NjYwMzY1ODg4N0FCMzM3M0EwOTMxQTlGQ0RGMTVGOTIxMzA4Mjg3RDY2NTNENjg2MzEwMTg3N0JEMEY4OTRFMEE1QUM2MUExMTk5MTJCNTREMzhBQkI1QTU2QjM2M0RDODRFMDgxMENFQzhCMjc4ODYzNzg2QjU2RTA1NkIyNDg4QkQyNzNCMjU2RDQwQzRFMkMwNDRFOTIyMUJEMTQ1NDM4NDczNTY0MEU4QTQ4RkFDNTIyRDg2RUFDMTk5NjZCMjU0ODIzMjEyMzkxRkNGRDAwRTM4NTdFQkRGN0RCNjcyRDgyNzNDODEwRTRBRTgyQTRBMkNEMkY5Q0ZEQzFFNzJDNUQ3QjlEMUEzMDVCMDhCMEVDOUEzRjA0QkFEOEZDRjREOUYyRURCOUYzREZFMjI0ODJDMkJCQTJFRjQ4REM3NjVBODMzOEJF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EyOTM2NUFCNkMzRjMzOUE4NkYwNzA5MkYzQzI2ODkyQUIxOTZFNDlFMUFDQzQ5Qzc4MkJFODIzNzA1QkNFOTMzQjM0Rjc5NzJCMUIwNzZGRDExM0QyRTBDMEY0RUNENEEyNEVDREIwNDdDNjhFOEM4RUY1NDcyOTVGRUJCQjhFNTZEQUMxQThEQjg5N0NDRkVGQ0RFNjE0REU2Njg1NjJGMTg0QTQxMzBCMjA3NDRERUU1MTYxMjJFMjU2QkM2ODE2Mzg3OTNCN0ZCNzM4NjM3NjFDQjMzRjhBRDBENzg1MUQ1MkFCNjE3RjYxNkFDQTQ4RDRCRjZGQTM0QTYwODI1NjY0RERGN0YyNDc3QjU1MzlCOEFBMDhFRUU5NEUzMzg1NTFCRUMxREM1MDcyNjJFRTcwMTdFMzU1RDdCRDhBMkNBQ0Q3MTQyODczNUFCMTFEN0U0RjUzOEQ2QkI1NzdDNzBDMTJGQ0U1MkEyRTU5MjE5NkY5NTQyQ0M2RUYyQjBFMDUwNEE1RkVGNUY3RkVDQUY0QkU0OTZDMEEzODFFRTE3OUYwQTFD&instanceId=I000000000000000001&mobKey=Awp+PR4dO94HojuA0rS4X+cluWahGoaQcybSXMsrkbcYkAAfmBwD9YNszzWcDK/s92do8rn2sm3dCSEgpRdJxruSsbB5kqqOQWKRy4quYLCTmDEyN/P2hc0Fe17FfmAd4+G5W1CGILUSxO3s8RgvTjcqhxYnfwMUcJoCoIywVE8=&notifyUrl=www.baidu.com&timestamp=1231239233";
    static String sign = "cV9bcqDCXa+7VhS3p5iHokqc5ZMo8jAOknrvOWN8PDJPGbiJV2zQNDjp63Yw+1KwuiPGLCREOAy9aiQtgxF3ULw/ixsaERMOrny86+eamUWTNSMP/2JoIIAlIY5ZmCMRnsqGbLfH/n4/PvidNKO4UCIxrdUI7Ipv6X0fTtu7pes=";
    public static void main(String[] args) {
        try {
            byte[] b = (new sun.misc.BASE64Decoder()).decodeBuffer(publickey);
            X509EncodedKeySpec keySpec = new X509EncodedKeySpec(b);
            KeyFactory factoty = KeyFactory.getInstance("RSA");
            PublicKey pubKey = factoty.generatePublic(keySpec);
            Signature signature = Signature.getInstance("SHA256WithRSA");
            signature.initVerify(pubKey);
            signature.update(data.getBytes("UTF-8"));
            boolean bverify = signature.verify(Base64.decode(sign));
            System.out.println(bverify);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
